//
//  main.c
//  MasteringAlgorithms
//  Illustrates data encryption (see Chapter 15).
//
//  Created by YourtionGuo on 22/05/2017.
//  Copyright © 2017 Yourtion. All rights reserved.
//

#include <stdio.h>
#include <string.h>

#include "encrypt.h"

int main(int argc, char **argv)
{
    unsigned char   destmp[8], desptx[8], desctx[8], deskey[8];

    Huge            rsatmp, rsaptx, rsactx;
    RsaPubKey       rsapubkey;
    RsaPriKey       rsaprikey;
    int             i, error = 0;



    /// 执行 RSA 加解密操作

    fprintf(stdout, "Enciphering with RSA\n");

    rsapubkey.e = 17;
    rsapubkey.n = 209;
    rsaprikey.d = 53;
    rsaprikey.n = 209;

    fprintf(stdout, "-> d=%lu, e=%lu, n=%lu\n", rsaprikey.d, rsapubkey.e, rsapubkey.n);

    for (i = 0; i < 128; i++) {

        rsatmp = i;
        rsa_encipher(rsatmp, &rsactx, rsapubkey);
        rsa_decipher(rsactx, &rsaptx, rsaprikey);

        if (rsatmp == rsaptx) {
            fprintf(stdout, "--> rsatmp=%5lu, rsactx=%5lu, rsaptx=%5lu (OK)\n", rsatmp, rsactx, rsaptx);
        } else {
            error++;
            fprintf(stdout, "--> rsatmp=%5lu, rsactx=%5lu, rsaptx=%5lu (ERROR)\n", rsatmp, rsactx, rsaptx);
        }
    }

    if (error == 0) {
        fprintf(stdout, "RSA Enciphering OK\n");
    } else {
        fprintf(stdout, "RSA Enciphering Error: %d\n", error);
    }

    return 0;
}
